package com.mxpio.erp.asset.controller;

import com.mxpio.erp.asset.purchase.entity.InitializeReceivingNoticeImportModel;
import com.mxpio.erp.asset.purchase.entity.ReceivingNoticeImportModel;
import com.mxpio.erp.asset.service.CommonUploadService;
import com.mxpioframework.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author : 吴盼盼
 * @Datetime : 2022/10/14 11:22
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc :
 */
@Tag(name = "AssetBorrowApplyController", description = "资产个性化导入统一接口")
@RestController
@RequestMapping("/erp/asset/excel/")
public class AssetCommonUploadController {

    @Autowired
    private CommonUploadService commonUploadService;

    @PostMapping("uploadReceivingnotice")
    @Operation(summary = "到货通知单导入", description = "到货通知单导入")
    public Result<?> uploadReceivingnotice(HttpServletRequest request) {
        try{
            MultipartHttpServletRequest multipartRequest =null;
            if (request instanceof MultipartHttpServletRequest) {
                multipartRequest = (MultipartHttpServletRequest)(request);
            }else {
                return Result.error("请在请求头中设置key:Content-Type,value:multipart/form-data");
            }
            MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
            List<ReceivingNoticeImportModel> readModelList =  commonUploadService.uploadReceivingnotice(file.getInputStream());
            int yesSize = 0;
            int successSize = 0;
            int errorSize = 0;
            for (ReceivingNoticeImportModel model : readModelList) {
                yesSize = yesSize + 1;
                if ("失败".equals(model.getImportSuccess())) {
                    errorSize = errorSize + 1;
                } else {
                    successSize = successSize + 1;
                }
            }
            return Result.OK("本次一共处理【"+ yesSize+ "】条，不通过【"+ errorSize+"】条，"+ "通过【"+ successSize+ "条】。",readModelList);
        } catch (Exception e) {
            return Result.error("导入失败");
        }
    }

    @PostMapping("uploadInitializeReceivingnotice")
    @Operation(summary = "初始入库导入", description = "初始入库导入")
    public Result<?> uploadInitializeReceivingnotice(HttpServletRequest request) {
        try{
            MultipartHttpServletRequest multipartRequest =null;
            if (request instanceof MultipartHttpServletRequest) {
                multipartRequest = (MultipartHttpServletRequest)(request);
            }else {
                return Result.error("请在请求头中设置key:Content-Type,value:multipart/form-data");
            }
            MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
            List<InitializeReceivingNoticeImportModel> readModelList =  commonUploadService.uploadInitializeReceivingnotice(file.getInputStream());
            int yesSize = 0;
            int successSize = 0;
            int errorSize = 0;
            for (InitializeReceivingNoticeImportModel model : readModelList) {
                yesSize = yesSize + 1;
                if ("失败".equals(model.getImportSuccess())) {
                    errorSize = errorSize + 1;
                } else {
                    successSize = successSize + 1;
                }
            }
            return Result.OK("本次一共处理【"+ yesSize+ "】条，不通过【"+ errorSize+"】条，"+ "通过【"+ successSize+ "条】。",readModelList);
        } catch (Exception e) {
            return Result.error("导入失败");
        }
    }
}
